﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace TheJoyOfCode.SLUploader
{
    public class FileUploaderPresenter
    {
        private readonly FileUploaderViewModel _viewModel;
        public readonly IFileUploadView _view;

        public FileUploaderPresenter(IFileUploadView view)
        {
            _view = view;
            _viewModel = new FileUploaderViewModel();
        }

        public FileUploaderViewModel ViewModel
        {
            get { return _viewModel; }
        }

        public void Upload()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = true;
            dlg.Filter = _view.Filter;
            if (dlg.ShowDialog().GetValueOrDefault())
            {
                ViewModel.Status = string.Empty;

                Uri uri = new Uri(_view.ServiceUri, UriKind.RelativeOrAbsolute);

                if (!uri.IsAbsoluteUri)
                {
                    uri = new Uri(_view.DocumentUri, uri);
                }

                IUploader uploader;
                if (_view.UseDummyService)
                {
                    uploader = new DummyUploader(_view);
                }
                else
                {
                    uploader = new Uploader(_view, uri);
                }
                uploader.FileUploadStarted += new EventHandler<FileUploadStartedEventArgs>(uploader_FileUploadStarted);
                uploader.FileUploadComplete += new EventHandler<FileUploadedCompletedEventArgs>(uploader_FileUploaded);
                uploader.UploadsComplete += new EventHandler<UploadsCompleteEventArgs>(uploader_UploadsComplete);
                Queue<FileInfo> files = new Queue<FileInfo>(dlg.Files);
                uploader.UploadFilesAsync(files);

                ViewModel.TotalFiles = files.Count;
                ViewModel.UploadedFiles = 0;

                ViewModel.ShowOKButton = false;
                ViewModel.ShowUploadButton = false;
            }
        }

        public void Done()
        {
            ViewModel.UploadedFiles = 0;
            ViewModel.TotalFiles = 1;
            ViewModel.ShowUploadButton = true;
            ViewModel.ShowOKButton = false;
        }

        void uploader_FileUploadStarted(object sender, FileUploadStartedEventArgs e)
        {
            _view.Dispatcher.BeginInvoke(delegate()
            {
                ViewModel.Status = string.Format(
                    "Uploading {0}", 
                    e.FileInfo.Name);
            });
        }

        void uploader_FileUploaded(object sender, FileUploadedCompletedEventArgs e)
        {
            _view.Dispatcher.BeginInvoke(delegate()
            {
                ViewModel.UploadedFiles = e.UploadedFiles;
            });
        }

        void uploader_UploadsComplete(object sender, UploadsCompleteEventArgs e)
        {
            _view.Dispatcher.BeginInvoke(delegate()
            {
                ViewModel.Status = string.Format("Done. Uploaded {0}/{1} files.", e.UploadedFiles, e.TotalFilesToUpload);
                ViewModel.ShowOKButton = true;
            });
        }
    }
}
